package com.clkj.xiuzheng.service.shangcheng;

import com.clkj.xiuzheng.dao.shangcheng.ScFactoryMapper;
import com.clkj.xiuzheng.pojo.shangcheng.ProductFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

@Service
public class ScFactoryServiceImpl implements ScFactoryService{

    @Autowired
    private ScFactoryMapper scFactoryMapper;

    @Override
    public List<ProductFactory> getFactory(String factoryName, Integer CompanyId) {
          Example example =  new Example(ProductFactory.class);
          Example.Criteria criteria = example.createCriteria();
          if (CompanyId!=null){
              criteria.andEqualTo("companyId",CompanyId);
          }
          if (factoryName!=null){
              criteria.andCondition(" factory_name like '%"+factoryName+"%'");
          }
        List<ProductFactory> scFactories = scFactoryMapper.selectByExample(example);
        return scFactories;
    }

    @Override
    public void addFactory(ProductFactory productFactory) {
        scFactoryMapper.insertSelective(productFactory);
    }

    @Override
    public void updateFactoryStatus(String ids, Integer status) {
        String[] idList = ids.split(",");
        ProductFactory factory = new ProductFactory();
        factory.setFactoryStatus(status);
        Example example = new Example(ProductFactory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("factoryId", Arrays.asList(idList));
        scFactoryMapper.updateByExampleSelective(factory,example);
    }

    @Override
    public void updateFactoryName(String name, Integer id) {
        ProductFactory factory = new ProductFactory();
        factory.setFactoryName(name);
        factory.setFactoryId(id);
        scFactoryMapper.updateByPrimaryKeySelective(factory);
    }


}
